package com.ze.familydayverlenovo.adapter;

import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.ze.commontool.LoadImageMgr;
import com.ze.commontool.NetHelper;
import com.ze.commontool.PublicInfo;
import com.ze.commontool.LoadImageMgr.ImageCallBack;
import com.ze.familydayverlenovo.R;
import com.ze.familydayverlenovo.R.id;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class DialogDetailListViewAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<Map<String, Object>> list;
	public static final String flag[] = {
		"head","say","time","item_type","vip"
	};
	
	public static final int 		ids[] = {
		R.id.dialog_detail_me_head,
		R.id.dialog_detail_me_say,
		R.id.dialog_detail_me_time,
		R.id.dialog_detail_me_head_vip
	};
	public static final int 		id_othser[] = {
		R.id.dialog_detail_other_head,
		R.id.dialog_detail_other_say,
		R.id.dialog_detail_other_time,
		R.id.dialog_detail_other_head_vip,
	};
	
	public static final int 		card_ids[] = {
		R.id.dialog_card_me_head,
		R.id.dialog_card_me_time,
		R.id.dialog_card_me_head_vip,
		R.id.dialog_card_me_card_head,
		R.id.dialog_card_me_card_head_vip,
		R.id.dialog_card_me_card_name,
	};
	public static final int 		card_id_othser[] = {
		R.id.dialog_card_other_head,
		R.id.dialog_card_other_time,
		R.id.dialog_card_other_head_vip,
		R.id.dialog_card_other_card_head,
		R.id.dialog_card_other_card_head_vip,
		R.id.dialog_card_other_card_name,
	};
	
	public static final int 		image_ids[] = {
		R.id.dialog_image_me_head,
		R.id.dialog_image_me_time,
		R.id.dialog_image_me_head_vip,
		R.id.dialog_image_me_context,
	};
	public static final int 		image_id_othser[] = {
		R.id.dialog_image_other_head,
		R.id.dialog_image_other_time,
		R.id.dialog_image_other_head_vip,
		R.id.dialog_image_other_context,
	};
	
	private int 		layout;
	private int      layout_type2;
	
	private int      layout_type_card_me;
	private int      layout_type_card_other;
	
	private int      layout_type_pic_me;
	private int      layout_type_pic_other;
	
	private int      layout_type_feed_me;
	private int      layout_type_feed_other;
	
	private Context mContext;
	private final int TYPE_ITEM_0 = 0;
	private final int TYPE_ITEM_1 = 1;
	public DialogDetailListViewAdapter(Context context, List<Map<String, Object>>list) {
		this.mInflater = LayoutInflater.from(context);
		this.list = list;
		
		this.layout = R.layout.dialog_detail_item_me;
		layout_type2 = R.layout.dialog_detail_item_other;
		
		layout_type_card_me = R.layout.dialog_card_item_me;
		layout_type_card_other = R.layout.dialog_card_item_other;
		
		layout_type_pic_me = R.layout.dialog_image_item_me;
		layout_type_pic_other = R.layout.dialog_image_item_other;
		
		mContext = context;
	}
	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;
	}
	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return (Integer)((Map<String, Object>)getItem(position)).get("item_type");
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list == null ? 0 :list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder ;
		JSONObject sayJsonObject = null;
		String tempType = "";
		try {
			if( ((Map<String, Object>)getItem(position)).containsKey("say") 
					)
			sayJsonObject = new JSONObject(
					(String)((Map<String, Object>)getItem(position)).get("say"));
			tempType = sayJsonObject.optString("type");
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if( convertView == null || 
				! ((Holder) convertView.getTag()).type.equals(tempType))
		{
			holder = new Holder();
			// @ze 2013.11.9 对话详情添加类型
			if( sayJsonObject != null && !sayJsonObject.isNull("type") )
			{
				if (sayJsonObject.optString("type")
						.equals("card")) {
					holder.type = "card";
					if(getItemViewType(position) == 0){
						convertView = mInflater.inflate(layout_type_card_other, null);
						holder.head = (ImageView)convertView.findViewById(card_id_othser[0]);
	//					holder.name = (TextView)convertView.findViewById(card_id_othser[1]);
						holder.time	= (TextView)convertView.findViewById(card_id_othser[1]);
						holder.headvip = (ImageView)convertView.findViewById(card_id_othser[2]);
						holder.card_head = (ImageView)convertView.findViewById(card_id_othser[3]);
						holder.card_headvip = (ImageView)convertView.findViewById(card_id_othser[4]);
						holder.card_name = (TextView)convertView.findViewById(card_id_othser[5]);
					}else
					{
						convertView = mInflater.inflate(layout_type_card_me, null);
						holder.head = (ImageView)convertView.findViewById(card_ids[0]);
	//					holder.name = (TextView)convertView.findViewById(card_id_othser[1]);
						holder.time	= (TextView)convertView.findViewById(card_ids[1]);
						holder.headvip = (ImageView)convertView.findViewById(card_ids[2]);
						holder.card_head = (ImageView)convertView.findViewById(card_ids[3]);
						holder.card_headvip = (ImageView)convertView.findViewById(card_ids[4]);
						holder.card_name = (TextView)convertView.findViewById(card_ids[5]);
					}
				}else if(sayJsonObject.optString("type")
						.equals("image")){
					holder.type = "image";
					if(getItemViewType(position) == 0){
						convertView = mInflater.inflate(layout_type_pic_other, null);
						holder.head = (ImageView)convertView.findViewById(image_id_othser[0]);
	//					holder.name = (TextView)convertView.findViewById(card_id_othser[1]);
						holder.time	= (TextView)convertView.findViewById(image_id_othser[1]);
						holder.headvip = (ImageView)convertView.findViewById(image_id_othser[2]);
						holder.image_context = (ImageView)convertView.findViewById(image_id_othser[3]);
					}else
					{
						convertView = mInflater.inflate(layout_type_pic_me, null);
						holder.head = (ImageView)convertView.findViewById(image_ids[0]);
	//					holder.name = (TextView)convertView.findViewById(card_id_othser[1]);
						holder.time	= (TextView)convertView.findViewById(image_ids[1]);
						holder.headvip = (ImageView)convertView.findViewById(image_ids[2]);
						holder.image_context = (ImageView)convertView.findViewById(image_ids[3]);
					}
				}else
				{
					holder.type = "unknown";
					// 未识别类型
					if(getItemViewType(position) == 0)
					{
						// other
						convertView = mInflater.inflate(layout_type2, null);
						holder.head = (ImageView)convertView.findViewById(id_othser[0]);
						holder.name = (TextView)convertView.findViewById(id_othser[1]);
						holder.time	= (TextView)convertView.findViewById(id_othser[2]);
						holder.headvip = (ImageView)convertView.findViewById(id_othser[3]);
					}else
					{
						convertView = mInflater.inflate(layout, null);
						holder.head = (ImageView)convertView.findViewById(ids[0]);
						holder.name = (TextView)convertView.findViewById(ids[1]);
						holder.time	= (TextView)convertView.findViewById(ids[2]);
						holder.headvip = (ImageView)convertView.findViewById(ids[3]);
					}
				}
			}else{
				holder.type = "";
				// 普通文字类型
				if(getItemViewType(position) == 0)
				{
					// other
					convertView = mInflater.inflate(layout_type2, null);
					holder.head = (ImageView)convertView.findViewById(id_othser[0]);
					holder.name = (TextView)convertView.findViewById(id_othser[1]);
					holder.time	= (TextView)convertView.findViewById(id_othser[2]);
					holder.headvip = (ImageView)convertView.findViewById(id_othser[3]);
				}else
				{
					convertView = mInflater.inflate(layout, null);
					holder.head = (ImageView)convertView.findViewById(ids[0]);
					holder.name = (TextView)convertView.findViewById(ids[1]);
					holder.time	= (TextView)convertView.findViewById(ids[2]);
					holder.headvip = (ImageView)convertView.findViewById(ids[3]);
				}
			}
			convertView.setTag(holder);
		}else {
			holder = (Holder) convertView.getTag();
		}
		holder.head.setImageDrawable(LoadImageMgr.getInstance().loadDrawble((String)list.get(position).get(flag[0]), 
				holder.head, LoadImageMgr.getInstance().imageCallBack));
		if(( (String)list.get(position).get(flag[2]) ).equals("0") )
		{
			holder.time.setText("发送中");
		}else if(( (String)list.get(position).get(flag[2]) ).equals("-1") )
		{
			holder.time.setText("发送失败");
		}else{
			holder.time.setText( NetHelper.transTime(Long.parseLong( (String)list.get(position).get(flag[2]) ) ) );
		}
		if( list.get(position).get(flag[4])  .equals(PublicInfo.VIP_FLAG_F ))
		{
			holder.headvip.setImageResource(R.drawable.v_l_2);
		}else if( list.get(position).get(flag[4])  .equals(PublicInfo.VIP_FLAG_P )){
			holder.headvip.setImageResource(R.drawable.v_l_1);
		}
		if( sayJsonObject != null && !sayJsonObject.isNull("type") )
		{
			if( sayJsonObject.optString("type")
					.equals("card") )
			{
				try {
					JSONObject object = new JSONObject( (String)list.get(position).get(flag[1]) );
					if( ! object.isNull("avatar") )
						holder.card_head.setImageDrawable(
								LoadImageMgr.getInstance().loadDrawble( object.optString("avatar", "") , 
							holder.card_head, LoadImageMgr.getInstance().imageCallBack));
					
					if( object.optString("vipstatus", "0")  .equals(PublicInfo.VIP_FLAG_F ))
					{
						holder.card_headvip.setImageResource(R.drawable.v_l_2);
					}else if( object.optString("vipstatus", "0")  .equals(PublicInfo.VIP_FLAG_P )){
						holder.card_headvip.setImageResource(R.drawable.v_l_1);
					}
					
					holder.card_name.setText( object.optString("name", "无") );
					
					
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				
			}else if( sayJsonObject.optString("type")
					.equals("image") ) {
				try {
					JSONObject object = new JSONObject( (String)list.get(position).get(flag[1]) );
					if( ! object.isNull("imgurl") )
					{
						String imgurl = object.optString("imgurl");
//						if (imgurl.contains("!120X120")) {
//							imgurl = imgurl.replace("!120X120", "");
//						}
						holder.image_context.setImageDrawable(
								LoadImageMgr.getInstance().loadDrawble( imgurl , 
							holder.image_context, imageCallBack));
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}else
			{
				// 未识别类型
				holder.name.setText(mContext.getResources().getString(R.string.version_unknown) );
			}
		}else
		{
			holder.name.setText( (String)list.get(position).get(flag[1]) );
		}
			
		
		return convertView;
	}
	
	public ImageCallBack imageCallBack = new ImageCallBack() {
		
		@Override
		public void setImage(Drawable d, String url, ImageView view) {
			// TODO Auto-generated method stub
			if( /*mActiveImages.contains(view) &&*/ url.equals((String)view.getTag()))
			{
//				int height = d.getIntrinsicHeight();
//				int width  = d.getIntrinsicWidth();
//				LayoutParams params = (LayoutParams) view.getLayoutParams();
//				params.height = height;
//				params.width = width;
//				view.setLayoutParams(params);
				view.setImageDrawable(d);
			}
		}
	};
	static class Holder
	{
		ImageView	head;
		TextView 	name;
		TextView 		time;
		ImageView 	headvip;
		
		ImageView 	card_head;
		TextView 	card_name;
		ImageView 	card_headvip;
		
		ImageView 	image_context;
		
		String 		type;
	}
	
}
